<?php

class M_menu extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	function create($menu,$url){
		$data = array(
		'menu' =>$menu,
		'url'=> $url
		);
	
		$this->db->insert('managemen_menu', $data);
		if ($this->db->affected_rows() == 1) {
			return TRUE;
		}
		return FALSE;
	}
	
	function getTable($perPage = 0, $offset = 0) {
        $this->db->select('*');
		$this->db->from('managemen_menu');
		$this->db->order_by('id_menu', 'DESC');
        $this->db->limit($perPage, $offset);
        $query = $this->db->get();

        return $query->result_array();
        
    }
	
	function getAllData(){
		$this->db->select('*');
		$this->db->order_by('id_menu', 'DESC');
        $query = $this->db->get('managemen_menu');
		
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
	}
	
	function getDataById($id){
		$this->db->select('*');
		$this->db->where('id_menu', $id);
		$query = $this->db->get('managemen_menu');
		//if($query->num_rows() == 1){
			//return $query->result_array();
		//}
		//$query = $this->db->query("select * from materi a join divisi b on a.id_divisi=b.id_divisi where a.id_materi='".$id."' order by a.id_materi");
		return $query->result_array();

	}
	
	function update($id,$menu,$url, $status){
		$data = array(
		'menu' =>$menu,
		'url'=> $url,
		'status' => $status
		);
		$this->db->where('id_menu', $id);
		$this->db->update('managemen_menu', $data);
		
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function delete($id){
		$this->db->where('id_menu', $id);
		$this->db->delete('managemen_menu');
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function setEnable($id, $status){
		$data = array(
		'status'=> $status
		);
		$this->db->where('id_menu', $id);
		$this->db->update('managemen_menu', $data);
		
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function setDisable($id, $status){
		$data = array(
		'status'=> $status
		);
		$this->db->where('id_menu', $id);
		$this->db->update('managemen_menu', $data);
		
		if ($this->db->affected_rows() == 1) {

            return TRUE;
        }
        return FALSE;
	}
	
	function search($key){
		$this->db->select('*');
		$this->db->like('menu', $key);
		$this->db->order_by('id_menu', 'DESC');
		$query = $this->db->get ('managemen_menu');
		return $query->result_array();
	}
	
}